/**
 * YunjButton
 */
layui.define(['jquery'], function (exports) {
    let win = window;
    let doc = document;
    let $ = layui.jquery;

    class YunjButton {

        constructor(args) {
            args = this.handleArgs(args);
            this.args = args;

            this.type = args.type;             // 类型
            this.text = args.text;      // 描述
            this.class = args.class;    // class标记
            this.icon = args.icon;      // 图标
            this.formId = args.formId;  // 表单id
        }

        // 处理args
        handleArgs(args) {
            args = Object.assign({}, {
                text: '',
                class: '',
                icon: '',
                mobileDropDown: false,
                formId: '',
            }, args);
            return args;
        }

        /**
         * 渲染
         * @param boxFilter    渲染所在容器的filter
         * @param outLayout    外部结构 当传入外部结构时，如：<div>__layout__</div>，会用生成的结构替换掉关键词 __layout__
         */
        render(boxFilter = '', outLayout = '') {
            let that = this;
            let layout = that.layout();
            if (yunj.isString(outLayout) && outLayout.indexOf('__layout__') !== -1) layout = outLayout.replace('__layout__', layout);
            $(boxFilter).append(layout);
            return that;
        }

        /**
         * 布局
         * @param isDropdown 是否下拉
         * @returns {string}
         */
        layout(isDropdown = false) {
            let that = this;

            let iconClass = yunj.iconClass(that.icon);
            let icon = iconClass ? `<i class="${iconClass}"></i> ` : '';
            return isDropdown ? `<dd title="${that.text}" class="yunj-btn-${that.type}">${that.text}</dd>`
                : `<button type="button" class="layui-btn layui-btn-sm yunj-btn-${that.type} ${that.class}" title="${that.text}">${icon} ${that.text}</button>`;
        }

    }


    let button = (args = {}) => {
        return new YunjButton(args);
    }

    exports('button', button);
});